package com.ck.juc;

import lombok.SneakyThrows;

import java.util.concurrent.Semaphore;

/**
 * 信号量阻塞
 *  初始话一个指定大小的资源，供多个线程去抢占资源，抢到资源的线程执行玩后会释放资源，
 *  供其它线程去抢占并执行
 */
public class SemaphoreDemo {

    public static void main(String[] args) throws InterruptedException {
        Semaphore semaphore = new Semaphore(3);

        for(int index = 1;index <= 6;index++){
            new Thread(new Runnable() {
                @SneakyThrows
                @Override
                public void run() {
                    semaphore.acquire();
                    System.out.println(Thread.currentThread().getName() + "\t 抢到车位");
                    Thread.sleep(1000 * 3);
                    System.out.println(Thread.currentThread().getName() + "\t 离开");
                    semaphore.release();
                }
            },String.valueOf(index)).start();
        }
    }
}
